<?php

/**
 * index
 * @author xiaofeng 
 * @version 1.0
 * @package application
 */
class news extends Front_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
        $this->load->model('news_class_model');
    }

//    //频道页
//    public function channel($topid, $pid = 0) {
//
//        if (intval($topid) <= 0 || !is_numeric($pid) || intval($pid) < 0) {
//            header('location:' . base_url());
//            exit;
//        }
//        //赋值，用于区别父类还是子类
//        $tmpid = $pid ? $pid : $topid;
//        $data = array();
//        //父类$nodepath
//        $nodepath = $this->news_class_model->get_value_by_pk($tmpid, 'nodepath');
//        if (!$nodepath) {
//            header('location:' . base_url());
//            exit;
//        }
//        //取出所有父类名称
//        $data['parents'] = $this->news_class_model->get_parents_name($nodepath . $tmpid . ',');
//        $search = array(
//            'attributes' => array('recycled' => 0, 'parentid' => $topid),
//            'orders' => array('depth' => 'asc', 'created' => 'desc'),
//        );
//        //子类所有类名
//        $data['newssort_data'] = $this->news_class_model->get_values('id', 'sortname', null, $search);
//        //搜索查询
//        $search_post = $this->input->post();
//        if ($search_post['title'] && $search_post['title'] != '在这里搜索...') {
//            switch ($search_post['searchtype']) {
//                //检索标题
//                case 0:
//                    $search['attributes']['title'] = $search_post['title'];
//                    break;
//                //智能模糊
//                case 1:
//                    $search['attributes']['title_like'] = $search_post['title'];
//                    break;
//                default:
//                    header('location:' . base_url());
//                    exit;
//            }
//        }
//        $data['search_post'] = $search_post;
//        unset($search['attributes']['parentid'], $search_post);
//        $search['attributes']['nodepath_like'] = ',' . $tmpid . ',';
//
//        // 总数与分页
//        $total = $this->news_model->total(null, $search);
//        $data['total'] = $total;
//        // 请使用config_item( 'per_page' )获取全局显示条数
//        $per_page = config_item('per_page');
//        $this->load->library('pagination');
//        $url = $topid . '/' . $pid;
//        $pagination_config = array(
//            'base_url' => base_url($this->_site_path . '/news/channel/' . $url),
//            'total_rows' => $total,
//            'per_page' => $per_page,
//            'uri_segment' => 6,
//        );
//        //分页
//        $this->pagination->initialize($pagination_config);
//        $data['pagination'] = $this->pagination->create_links();
//        $search['limit'] = array('persize' => $per_page, 'offset' => $this->pagination->get_cur_offset());
//        $fields = array('id', 'title', 'created');
//        $data['lists'] = $this->news_model->get_appoint_values($fields, null, $search);
//
//        $data['topid'] = $topid;
//        $data['pid'] = $pid;
//        $this->load->view($this->_site_path . '/news/channel', $data);
//    }

    public function index($pid = 0) {
        $this->lists($pid);
    }

    public function lists($pid = 0) {

        $search['attributes']['nodepath_like'] = ',' . $pid . ',';
        // 总数与分页
        $total = $this->news_model->total(null, $search);
        $data['total'] = $total;
        // 请使用config_item( 'per_page' )获取全局显示条数
        $per_page = config_item('per_page');
        $per_page = 10;
        $this->load->library('pagination');
        $pagination_config = array(
            'base_url' => base_url($this->_site_path . '/news/index/' . $pid),
            'total_rows' => $total,
            'per_page' => $per_page,
            'uri_segment' => 5,
        );
        $pagination_config = array_merge($pagination_config, $this->pagination_config);
        //分页
        $this->pagination->initialize($pagination_config);
        $data['pagination'] = $this->pagination->create_links();
        $search['limit'] = array('persize' => $per_page, 'offset' => $this->pagination->get_cur_offset());
        $fields = array('id', 'title', 'imagemark', 'article_source', 'clickcount', 'description', 'modified');
        $data['news'] = $this->news_model->get_appoint_values($fields, null, $search);
        $data['newsClass'] = $this->news_class_model->get_by_pk($pid);

        $data['pid'] = $pid;
        $this->load->view($this->_site_path . '/news/index', $data);
    }

    public function single($id = 0) {
        $data['info'] = $this->news_model->get_by_pk($id);
        if (!$data['info']) {
            header('location:' . base_url($this->_site_path . '/index/index'));
            exit;
        }
        $data['pre'] = $this->news_model->pre_news($data['info']['parentid'], $id);
        $data['next'] = $this->news_model->next_news($data['info']['parentid'], $id);
        //增加文章的点击次数
        $this->news_model->update_by_pk(array('clickcount' => $data['info']['clickcount'] + 1), $id);
        $this->load->view($this->_site_path . '/news/single', $data);
    }

}
